function requestLoading(url, params, type, success) {
  wx.showNavigationBarLoading()
  wx.request({
    url: url,
    data: params,
    header: {
      'Content-Type': 'application/json',
      'X-Client-Name': 'ggfwapp'
    },
    method: type,
    success: function(res) {
      if (res.statusCode == 200) {
        success(res.data)
      } else {
        if (res.statusCode == 401) {
          wx.showToast({
            title: res.data.error_description,
            icon: 'none',
            duration: 2000
          })
        } else if (res.statusCode == 406) {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        } else if(res.statusCode == 400) {
          wx.showToast({
            title: '用户名或密码错误！',
            icon: 'none',
            duration: 2000
          })
        } else {
          console.log(res)
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    }
  })
}

function requestTip(url, params, type, success) {
  wx.request({
    url: url,
    data: params,
    header: {
      'Content-Type': 'application/json'
    },
    method: type,
    success: function (res) {
      if (res.statusCode == 200) {
        wx.hideNavigationBarLoading()
        success(res.data)
      } else {
        wx.hideNavigationBarLoading()
        if (res.statusCode == 401) {
          wx.showToast({
            title: res.data.error_description,
            icon: 'none',
            duration: 2000
          })
        } else {
          console.log(res)
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    }
  })
}

function requestNormal(url, params, type, success) {
  wx.showNavigationBarLoading()
  // params.access_token = JSON.parse(wx.getStorageSync('token')).access_token
  wx.request({
    url: url,
    data: params,
    header: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer ' + JSON.parse(wx.getStorageSync('token')).access_token 
    },
    method: type,
    success: function(res) {
      if (res.statusCode == 200) {
        success(res.data)
        wx.hideNavigationBarLoading()
      } else {
        wx.hideNavigationBarLoading()
        console.log(res.data.message)
        if (res.statusCode == 406) {
          console.log(url.indexOf('/leg/cbxx'))
          if (url.indexOf('/leg/cbxx') == -1) {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000
            })
          }
        } else if (res.statusCode == 401) {
          wx.clearStorage()
          wx.navigateTo({
            url: '/pages/login/login',
          })
          wx.showToast({
            title: '登录已过期，请重新登录。',
            icon: 'none',
            duration: 2000
          })
        }
      }
    }
  })
}

module.exports = {
  requestLoading: requestLoading,
  requestNormal: requestNormal,
  requestTip: requestTip
}